<?php
namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Util\SqlServerUtil;
class WechatController extends Controller
{
    protected $response = [
        'code' => 200,
        'result' => null,
        'message' => '',
    ];

    protected $SqlServer = null;

    public function __construct(Request $request){
        $temp = SqlServerUtil::getModel($request);
        if($temp){
            $this->SqlServer = $temp['SqlServer'];
        }
    }

    public function getWechatOpenID($code){
        $url = "https://api.weixin.qq.com/sns/jscode2session?appid=".env('WX_APP_ID')."&secret=".env('WX_APP_SECRET')."&js_code=".$code."&grant_type=authorization_code";
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL, $url);//设置url属性
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $output = curl_exec($ch);//获取数据\
        curl_close($ch);//关闭curl

        
        // try{
        //     $content = curl_exec($ch);
        // } finally{
        //     curl_close($ch);
        // }
        return $output;
        //return ['appid' => env('WX_APP_ID'), 'secret' => env('WX_APP_SECRET'), 'code' => $code]; 金额  公司单号    单价 生成订单号
    }


    //通过openid 查询账号是否已经绑定
    public function checkUser($openid){
        $res = SqlServerUtil::get($this->SqlServer,"yd_user",['openid' => $openid]);
        if(count($res) != 0 && $res != null){
            $res = $res[0];
            $this->response['result'] = $res;
            $this->response['message'] = "登录成功";
        }else{
            $this->response['code'] = 400;
            $this->response['message'] = "请先绑定商家账号";
        }
        return $this->response;
    }


    //绑定账号
    public function bindWechatUser(Request $request){

        //先查询是否已经绑定过
        $isBind = SqlServerUtil::get($this->SqlServer, "yd_user", ["openid" => $request->get('openid')]);
        if(count($isBind) == 1 && $isBind != false){
            $isBind = $isBind[0];
            if($isBind['openid'] == $request->get('openid')){
                $this->response['message'] = "登录成功";
            }else{
                $this->response['code'] = 400;
                $this->response['message'] = "该微信已经被".$isBind['username']."绑定,请勿重复绑定!";
            }
            return $this->response;
        }

        //未绑定用户 验证账号密码是否正确 然后关联绑定到传入的openid
        $user = SqlServerUtil::get($this->SqlServer, "yd_user", ["username" => $request->get('username')]);
        if(count($user) == 0){
            $this->response['code'] = 400;
            $this->response['message'] = "账号错误，绑定失败请认真输入账号!";
        }else{
            $user = $user[0];
            if($user['openid']){
                $this->response['code'] = 400;
                $this->response['message'] = "该微信已经被".$request->get('username')."绑定,请勿重复绑定!";
            }else{
                $user = $user[0];
                //开始与微信openid绑定
                $res = SqlServerUtil::execSql($this->SqlServer,"UPDATE yd_user SET openid = '{$request->get('openid')}' WHERE username = '{$request->get('username')}'");
                if(count($res) == 0){
                    $this->response['message'] = "初始化绑定成功，下次通过该微信自动登录";
                }else{
                    $this->response['code'] = 400;
                    $this->response['message'] = "绑定失败，请重新绑定!";
                }
            }
        }
        return $this->response;
    }

    //注销用户
    public function unUser(Request $request){
        $fid = $request->get('fid');
        $res = TCUser::where(['FID' => $fid])->update(['wxid' => ""]);
        if($res){
            $this->response['message'] = "注销成功";
        }else{
            $this->response['code'] = 400;
            $this->response['message'] = "注销失败";
        }
        return response()->json($this->response);
    }

    //修改密码
    public function updatePassword(Request $request){
        $user = TCUser::where(['FSID' => $request->get('old_password'), 'FID' => $request->get('FID')])->first();
        if($user){
            $this->response['result'] = $user->update(['FSID' => $request->get('password')]);
        }else{
            $this->response['code'] = 400;
            $this->response['message'] = "旧密码不正确";
        }
        return response()->json($this->response);
    }
}
